/* Copyright 2010-2012 by the developers of the OWL-BGP project. 

   This file is part of OWL-BGP.

   OWL-BGP is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   OWL-BGP is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with OWL-BGP. If not, see <http://www.gnu.org/licenses/>.
 */

package  org.semanticweb.sparql.owlbgp.parser.triplehandlers.builtinpredicate;

import java.util.Set;

import org.semanticweb.sparql.owlbgp.model.Identifier;
import org.semanticweb.sparql.owlbgp.model.dataranges.DataRange;
import org.semanticweb.sparql.owlbgp.model.dataranges.Datatype;
import org.semanticweb.sparql.owlbgp.model.dataranges.DatatypeRestriction;
import org.semanticweb.sparql.owlbgp.model.dataranges.FacetRestriction;
import org.semanticweb.sparql.owlbgp.parser.TripleConsumer;
import org.semanticweb.sparql.owlbgp.parser.triplehandlers.TripleHandler;

public class TPOnDatatypeHandler extends TripleHandler {

    public TPOnDatatypeHandler(TripleConsumer consumer) {
        super(consumer);
    }

    @Override
    public void handleTriple(Identifier subject, Identifier predicate, Identifier object) {
        consumer.translateDataRange(object);
        DataRange dataRange=consumer.getDR(object);
        if (dataRange!=null && dataRange instanceof Datatype) {
            Datatype datatype=(Datatype)dataRange;
            // get facet restrictions
            Set<FacetRestriction> facetRestrictions=consumer.translateToFacetRestrictionSet(subject);
            if (facetRestrictions!=null && facetRestrictions.size()>0)
                consumer.mapDataRangeIdentifierToDataRange(subject, DatatypeRestriction.create(datatype,facetRestrictions));
            else
                // TODO: error handling
                consumer.mapDataRangeIdentifierToDataRange(subject, datatype);
        } else
            // TODO: error handling
            throw new RuntimeException("error");
    }
}
